package com.chaopei.reflection;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

/**
 * @Author Corday 天涯
 * @Date 2022/3/15 22:47
 */
public class MyUser {
    @MyAnn(describe = "姓名", type = String.class)
    private String name;
    @MyAnn(describe = "等级", type = int.class)
    private int level;

    public MyUser(@MyAnn(describe = "姓名", type = String.class) String name, @MyAnn(describe = "姓名", type = int.class) int level) {
        this.name = name;
        this.level = level;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }
}
// 测一下
class  TestAnn{
    public static void main(String[] args) {
        MyUser mu = new MyUser("大山",20);
        try {
            Class c =  Class.forName("com.chaopei.reflection.MyUser");
            Field[] fields=c.getDeclaredFields();
            for(Field f:fields){
               Annotation[] ans= f.getAnnotations();
               for (Annotation temp:ans){
                   MyAnn ma = (MyAnn)temp;
                   System.out.println("注解:"+ma.describe());
               }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}